package advanced.io;

import java.io.*;

/**
 * 流;
 * 1. 字节流: inputstream outputstream
 * 2. 字符流: reader writer
 * 3. 文件流 file randomaccessfile
 */
public class L_Stream {
    static String rootPath = "C:\\Users\\DMLYS\\Documents\\prepare_for_work\\pfw\\java_l\\src\\main\\java\\primary\\io";

    public static void main(String[] args) throws IOException {
        getDatasByInputStream();
        writeByOutputSteam();
        getStrByReader();
        writeByWriter();
    }

    /**
     * 读取文件中的数据
     * 使用字节流操作
     */
    public static void getDataByInputStream() {
        InputStream is = null;
        //1. 创建源
        File src = new File("C:\\Users\\DMLYS\\Documents\\prepare_for_work\\pfw\\java_l\\src\\main\\java\\primary\\io\\abc");

        try {
            //2. select stream
            is = new FileInputStream(src);
            //3. 读取数据
            int data1 = is.read();
            int data2 = is.read();
            int data3 = is.read();
            int data4 = is.read();
            System.out.println((char) data1);
            System.out.println((char) data2);
            System.out.println((char) data3);
            System.out.println(data4);
            int temp;
            while ((temp = is.read()) != -1) {
                System.out.println((char) temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4. 释放资源
                if (null == is)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 一次读取字节流中读取多个字节
     */
    public static void getDatasByInputStream() {
        InputStream is = null;
        //1. 创建源
        File src = new File("C:\\Users\\DMLYS\\Documents\\prepare_for_work\\pfw\\java_l\\src\\main\\java\\primary\\io\\abc");

        try {
            //2. select stream
            is = new FileInputStream(src);
            //3. 读取数据
            byte[] car = new byte[1024 * 5];// 容器大小
            int len = -1;//实际读取到的数据长度
            while ((len = is.read(car)) != -1) {
                //读取到的字节数组--> 字符串(解码)
                String str = new String(car, 0, len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4. 释放资源
                if (null == is)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeByOutputSteam() throws IOException {
        File target = new File(rootPath + "/target.txt");
        //select stream
        OutputStream os = null;
        try {
            os = new FileOutputStream(target, true);
            //write out
            String msg = "io is so easy";
            os.write(msg.getBytes());//write out msg bytes array
            os.flush();

        } catch (FileNotFoundException e) {


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //release the source
            if (os != null) os.close();
        }
    }

    /**
     * 文件字符输入流
     */
    public static void getStrByReader() {
        File src = new File("C:\\Users\\DMLYS\\Documents\\prepare_for_work\\pfw\\java_l\\src\\main\\java\\primary\\io\\abc");
        Reader reader = null;
        try {
            reader = new FileReader(src);
            char[] flush = new char[1024];
            int len = -1;
            while ((len = reader.read(flush)) != -1) {
//                字符数组-->字符串

                String str = new String(flush, 0, len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 文件字符输出流
     */
    public static void writeByWriter() {
        File target = new File(rootPath + "/target.txt");
        //select stream
        FileWriter os = null;
        try {
            os = new FileWriter(target, true);
            //write out
            String msg = "io is so easy";
            os.write(msg);//直接写出字符串
            os.write(msg.toCharArray());//写出字符数组
            os.append(msg);
            os.flush();

        } catch (FileNotFoundException e) {


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //release the source
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
